import java.util.concurrent.TimeUnit;

/**
 * @author wcj
 * @Date 2021/6/8 10:52
 * @Version 1.0
 */
public class ThreadState {

    /**
     * 线程的状态:
     * New:初始化
     * Runnable(Ready,Running)
     * Waiting:等待
     * Timed_Waiting
     * Blocked
     * Terminated
     *
     * @param args
     */
    public static void main(String[] args) {
        new Thread(new TimeWaiting(), "TimeWaitingThread").start();
        new Thread(new Waiting(), "Waiting").start();
        new Thread(new Blocked(), "Blocked1").start();
        new Thread(new Blocked(), "Blocked2").start();
    }

    static class TimeWaiting implements Runnable {
        @Override
        public void run() {
            while (true) {
                SleepUtils.second(100);
            }
        }
    }

    static class Waiting implements Runnable {

        @Override
        public void run() {
            while (true) {
                synchronized (Waiting.class) {
                    try {
                        Waiting.class.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    static class Blocked implements Runnable {

        @Override
        public void run() {
            synchronized (Blocked.class) {
                while (true) {
                    SleepUtils.second(100);
                }
            }
        }
    }
}

class SleepUtils {
    public static void second(long mills) {
        try {
            TimeUnit.MILLISECONDS.sleep(mills);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
